/**
 * Title: MsgVerificationController.java
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2019-8-1 23:36
 * @description Project Name: Grote
 * @Package: com.srct.service.controller
 */
package com.sharp.authorization.controller.common;

import com.sharp.authorization.annotation.auth.Auth;
import com.sharp.authorization.annotation.captcha.CaptchaVerify;
import com.sharp.authorization.bo.sms.SendSmsReq;
import com.sharp.authorization.config.msg.IMsgVerification;
import com.sharp.authorization.service.sms.MsgVerificationService;
import com.sharp.authorization.service.sms.SmsService;
import com.sharp.authorization.vo.msg.MsgCodeReq;
import com.sharp.authorization.vo.msg.MsgCodeRes;
import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.constant.err.ErrCodeSys;
import com.sharp.kernel.utils.ReflectionUtil;
import com.sharp.kernel.validate.Validator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(value = "短信验证码", tags = "COMMON-短信验证码")
@RestController("MsgVerificationController")
@RequestMapping(value = "")
public class MsgVerificationController {

    @Resource
    private MsgVerificationService msgVerificationService;
    @Resource
    private SmsService smsService;

    @Auth
    @ApiOperation(value = "获取短信验证吗", notes = "利用token获取短信验证码")
    @RequestMapping(value = "/msgCode", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "phoneNumber", value = "电话号码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "msgCodeType", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "graphCode", value = "图形验证码", required = true)})
    @CaptchaVerify(value = "", field = "msgCodeType")
    public ResponseEntity<Response<MsgCodeRes>> getMsgCode(MsgCodeReq req) {
        IMsgVerification msgCodeType = msgVerificationService.getMsgCodeType(req.getMsgCodeType());
        Validator.assertNotNull(msgCodeType, ErrCodeSys.SYS_CONFIG_NOT_EXIST, "验证码类型");
        String code = msgVerificationService.generateMsgCode(req.getPhoneNumber(), msgCodeType);
        SendSmsReq smsSendReq = buildSendSmsReq(req.getPhoneNumber(), msgCodeType, code);
        smsService.sendSms(smsSendReq);
        return ResponseHandler.generateResponse(ReflectionUtil.copy(msgCodeType, MsgCodeRes.class));
    }

    private SendSmsReq buildSendSmsReq(String phoneNumber, IMsgVerification msgVerificationType, String code) {
        SendSmsReq req = new SendSmsReq();
        req.setPhoneNumbers(phoneNumber);
        req.setSendSmsType(msgVerificationType.name());
        Map<String, String> paramMap = new HashMap<>(1);
        paramMap.put("code", code);
        req.setParamMap(paramMap);
        return req;
    }
}
